package org.nerdpod.feed;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import org.jdom.Element;

/**
 * Provides convenience methods for reading the feed.
 * @author twilson
 *
 */
public class FeedHelper {
	
	private static final String feedUrlString = "http://haiti.ushahidi.com/feed/";

	/**
	 * Return a list of incidents from the feed. 
	 * @return
	 */
	public static List<IncidentBean> getIncidents() {
		List<IncidentBean> incidentList = new ArrayList<IncidentBean>();
		URL feedUrl = null;
		try {
			feedUrl = new URL(feedUrlString);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		SyndFeedInput input = new SyndFeedInput();

		SyndFeed feed;
		try {
			feed = input.build(new XmlReader(feedUrl));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FeedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		List<SyndEntry> entries = feed.getEntries();
		for (SyndEntry entry : entries) {
			IncidentBean incident = new IncidentBean();
			incident.setUri(entry.getUri());
			incident.setIncidentDate(entry.getPublishedDate());
			incident.setTitle(entry.getTitle());
			List<Element> foreignMarkups = (List<Element>) entry
					.getForeignMarkup();
			for (Element foreignMarkup : foreignMarkups) {
				if ("http://www.georss.org/georss".equals(foreignMarkup
						.getNamespaceURI())
						&& "point".equals(foreignMarkup.getName())) {
					incident.setLocation(foreignMarkup.getText());
				}
			}
			incidentList.add(incident);
		}
		return incidentList;
	}

	
	
	
	
	
	
	/**
	 * The rest of these methods are deprecated or test
	 * @return
	 */
	
	
	public static String updateFeed() {
		// prepare the table
		
		// open the feed
		try {
			final URL feedUrl = new URL(feedUrlString);
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedUrl));
			List<SyndEntry> entries = feed.getEntries();
			for (SyndEntry entry: entries) {
				
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FeedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// close the table
		
		return "Updated.";
	}
	

	public static String getFeedCsv() {
		StringBuffer result = new StringBuffer();
		try {

			final URL feedUrl = new URL(feedUrlString);
			SyndFeedInput input = new SyndFeedInput();

			SyndFeed feed = input.build(new XmlReader(feedUrl));
			List<SyndEntry> entries = feed.getEntries();
			for (SyndEntry entry : entries) {
				result.append(entry.getPublishedDate());
				result.append(",");
				result.append(entry.getTitle());
				result.append(",");
				result.append(entry.getUri());
				// result.append(",");
				// result.append(entry.getDescription().getValue());
				List<Element> foreignMarkups = (List<Element>) entry
						.getForeignMarkup();
				for (Element foreignMarkup : foreignMarkups) {
					if ("http://www.georss.org/georss".equals(foreignMarkup
							.getNamespaceURI())
							&& "point".equals(foreignMarkup.getName())) {
						result.append(",");
						result.append(foreignMarkup.getText());
					}
				}
				result.append("\n");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ERROR: " + ex.getMessage());
		}
		return result.toString();
	}

	public static void main(String[] args) {
		System.out.println(getFeedCsv());

	}

	public static void firstTry() {

		boolean ok = false;
		try {
			URL feedUrl = new URL("http://haiti.ushahidi.com/feed/");

			SyndFeedInput input = new SyndFeedInput();

			SyndFeed feed = input.build(new XmlReader(feedUrl));
			List<SyndEntry> entries = feed.getEntries();
			for (SyndEntry entry : entries) {
				System.out.println(entry.getTitle());
				System.out.println(entry.getUri());
				System.out.println(entry.getDescription().getValue());
				List<Element> foreignMarkups = (List<Element>) entry
						.getForeignMarkup();
				for (Element foreignMarkup : foreignMarkups) {
					System.out.println("Foreign markup "
							+ foreignMarkup.getNamespaceURI() + "/"
							+ foreignMarkup.getName() + " = "
							+ foreignMarkup.getText());

				}
				System.out.println("-----------------");
			}

			ok = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ERROR: " + ex.getMessage());
		}

		if (!ok) {
			System.out.println();
			System.out
					.println("FeedReader reads and prints any RSS/Atom feed type.");
			System.out
					.println("The first parameter must be the URL of the feed to read.");
			System.out.println();
		}
	}

}
